package org.ruanjj.multithread.JUC;

import java.util.concurrent.SynchronousQueue;

public class SynchronousQueueDemo {
    public static void main(String[] args) {
        SynchronousQueue<String> synQueue = new SynchronousQueue<>();

        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName()+"正在放数据");
                    synQueue.put("a");
                    System.out.println(Thread.currentThread().getName()+"放数据完成");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            },String.valueOf(i)).start();
        }


        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName()+"正在取数据");
                    System.out.println(Thread.currentThread().getName()+"取数据完成"+synQueue.take());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            },String.valueOf(i)).start();
        }
    }
}
